package server.config;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer {
    @SuppressWarnings("unused")
    @Resource
    private IPRecoder ipRecoder;

    @Resource
    private YamlConfig config;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(ipRecoder)
                .addPathPatterns("/**"); // 所有路径
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        String[] methods = config.getMethods().toArray(new String[0]);
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                WebMvcConfigurer.super.addCorsMappings(registry);
                registry.addMapping("/**")
                        .allowCredentials(false)
                        .allowedOrigins("http://127.0.0.1","http://127.0.0.1:82")
                        .allowedMethods(methods)
                        .maxAge(3600);
            }
        };
    }
}
